package com.jhgsys.internal.education.controller;

import com.jhgsys.internal.base.dto.BaseCautionaryLessonDto;
import com.jhgsys.internal.base.entity.BaseCautionaryLesson;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.constant.ModuleManagerPermissionConstant;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.education.entity.EducationCautionaryLesson;
import com.jhgsys.internal.education.service.IEducationCautionaryLessonService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 *  Controller
 *
 * @author yj
 * @date 2024-09-13 17:53:22
 */
@Slf4j
@Validated
@Controller
public class EducationCautionaryLessonController extends BaseController {

    @Autowired
    private IEducationCautionaryLessonService educationCautionaryLessonService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "educationCautionaryLesson")
    @RequiresPermissions("educationCautionaryLesson:view")
    public String educationCautionaryLessonIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("education/educationCautionaryLesson/educationCautionaryLesson");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "educationCautionaryLesson/add")
    @RequiresPermissions("educationCautionaryLesson:add")
    public String educationCautionaryLessonAdd(){
        return FebsUtil.view("education/educationCautionaryLesson/educationCautionaryLessonEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "educationCautionaryLesson/update/{id}")
    @RequiresPermissions("educationCautionaryLesson:edit")
    public String educationCautionaryLessonUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("educationCautionaryLesson", educationCautionaryLessonService.findEducationCautionaryLessonById(id));
        return FebsUtil.view("education/educationCautionaryLesson/educationCautionaryLessonEdit");
    }

    /**
     * 查询（所有）
     * @author yj
     * @date 2024-09-13 17:53:22
     */
    @GetMapping("educationCautionaryLesson")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:view")
    public FebsResponse getAllEducationCautionaryLessons(EducationCautionaryLesson educationCautionaryLesson) {
        return new FebsResponse().success().data(educationCautionaryLessonService.findEducationCautionaryLessonList(educationCautionaryLesson));
    }

    /**
     * 查询（分页）
     * @author yj
     * @date 2024-09-13 17:53:22
     */
    @GetMapping("educationCautionaryLesson/list")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:view")
    public FebsResponse educationCautionaryLessonList(QueryRequest request, EducationCautionaryLesson educationCautionaryLesson) {
        //如果不是模块管理员，只能查看本部门的警示
        if(!hasPermission(ModuleManagerPermissionConstant.BASECAUTIONARY)){
            educationCautionaryLesson.setStudyDeptId(getCurrentUser().getDeptId());
        }
        Map<String, Object> dataTable = getDataTable(this.educationCautionaryLessonService.findEducationCautionaryLessonPage(request, educationCautionaryLesson));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 EducationCautionaryLesson
     * @author yj
     * @date 2024-09-13 17:53:22
     */
    @Log("新增EducationCautionaryLesson")
    @PostMapping("educationCautionaryLesson")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:add")
    public FebsResponse addEducationCautionaryLesson(@RequestBody EducationCautionaryLesson educationCautionaryLesson) throws FebsException {
        try {
            this.educationCautionaryLessonService.createEducationCautionaryLesson(educationCautionaryLesson);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增EducationCautionaryLesson失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 EducationCautionaryLesson
     * @author yj
     * @date 2024-09-13 17:53:22
     */
    @Log("逻辑删除EducationCautionaryLesson")
    @GetMapping("educationCautionaryLesson/delete/{id}")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:delete")
    public FebsResponse deleteEducationCautionaryLesson(@PathVariable("id") String educationCautionaryLessonIds) throws FebsException {
        try {
            EducationCautionaryLesson educationCautionaryLesson = new EducationCautionaryLesson();
            educationCautionaryLesson.setDeletedBy(getCurrentUser().getUserName());
            educationCautionaryLesson.setDeletedTime(new Date());
            educationCautionaryLesson.setDeletedFlg(FebsConstant.INVALID);
            this.educationCautionaryLessonService.deleteEducationCautionaryLesson(educationCautionaryLesson,educationCautionaryLessonIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除EducationCautionaryLesson失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 EducationCautionaryLesson
    * @author yj
    * @date 2024-09-13 17:53:22
    */
    @Log("物理删除EducationCautionaryLesson")
    @GetMapping("educationCautionaryLesson/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:delete")
    public FebsResponse physicallyDeleteEducationCautionaryLesson(@PathVariable("id") String educationCautionaryLessonIds) throws FebsException {
        try {
            this.educationCautionaryLessonService.physicallyDeleteEducationCautionaryLesson(educationCautionaryLessonIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除EducationCautionaryLesson失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 EducationCautionaryLesson
     * @author yj
     * @date 2024-09-13 17:53:22
     */
    @Log("修改EducationCautionaryLesson")
    @PostMapping("educationCautionaryLesson/update")
    @ResponseBody
    @RequiresPermissions("educationCautionaryLesson:edit")
    public FebsResponse updateEducationCautionaryLesson(@RequestBody BaseCautionaryLessonDto baseCautionaryLessonDto) throws FebsException {
        try {
            EducationCautionaryLesson educationCautionaryLesson = BeanHelper.copyProperties(baseCautionaryLessonDto, EducationCautionaryLesson.class);
            educationCautionaryLesson.setUpdatedBy(getCurrentUser().getUserName());
            educationCautionaryLesson.setStatus("1");
            educationCautionaryLesson.setStudyDate(new Date());
            this.educationCautionaryLessonService.updateEducationCautionaryLesson(educationCautionaryLesson);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改EducationCautionaryLesson失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

}
